package cn.diancan.manager.security;

import java.lang.reflect.Method;
import java.net.URLEncoder; 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

import cn.diancan.exception.OutMsgException;
import cn.diancan.util.RequestUtil;
import cn.diancan.util.ResponseJSONUtil;
/** 
 * 权限检查
 * @author Sky 
 */
public class ManagerAuthInterceptor extends MethodFilterInterceptor{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected String doIntercept(ActionInvocation actionInvocation) throws Exception { 
		ActionProxy proxy = actionInvocation.getProxy();
		Class<? extends Object> clazz =  proxy.getAction().getClass();
		Method m = clazz.getMethod(proxy.getMethod());
		ManagerAuth userInfoAuth =m.getAnnotation(ManagerAuth.class);
		if(null!=userInfoAuth){
			HttpServletRequest request= ServletActionContext.getRequest();
			HttpServletResponse response=ServletActionContext.getResponse();
			Object userId = request.getSession().getAttribute(ManagerAuthUtil.SESSIONKEYNAME);
			if(null==userId){
				if(!userInfoAuth.isJson()){
					try {
						if(userInfoAuth.isCurrentUrl()){
							String redirectUrl =getBasePath(request)+RequestUtil.getRequestPath(request).substring(1)+"?"+request.getQueryString();
							redirectUrl = URLEncoder.encode(redirectUrl, "UTF-8");
							response.sendRedirect(getBasePath(request)+"manager/Login_toLogin?src=i&redirectURL="+redirectUrl);
							return null;
						}
						String redirectUrl = userInfoAuth.redirectUrl();
						if(!"".equals(redirectUrl)){
							if(0==redirectUrl.indexOf("http://")){
								
							}else if(0==redirectUrl.indexOf("/")){
								redirectUrl= getBasePath(request)+redirectUrl.substring(1);
							}else{
								redirectUrl= getBasePath(request)+redirectUrl;
							}
							redirectUrl = URLEncoder.encode(redirectUrl, "UTF-8");
							response.sendRedirect( getBasePath(request)+"manager/Login_toLogin?src=i&redirectURL="+redirectUrl); 
							return null;
						}
					} catch (Exception e) { 
						System.out.println(e.getMessage());
					}
					response.sendRedirect(getBasePath(request)+"manager/Login_toLogin");
					return null;
				}else{ 
					return ResponseJSONUtil.textString(userInfoAuth.failJson());
				}
			}
		}
		try {
		    return actionInvocation.invoke();
		} catch (OutMsgException e) { 
		    if(null!=userInfoAuth && userInfoAuth.isJson()){
			return ResponseJSONUtil.commonJson(e.getCode(), e.getMessage());
		    }
		    throw e;
		}
	} 
	
	
	public String getBasePath(HttpServletRequest request){
		if(80==request.getServerPort()){
			return  request.getScheme()+"://"+request.getServerName()+request.getContextPath()+"/";
		}else{
			return request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
		}
	} 

}
